import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
const _import = require('./import_' + process.env.NODE_ENV)
// 全局路由(无需嵌套上左右整体布局)
const globalRoutes = [
  // {path: '/login', component: _import('user/login'), name: 'login', meta: {title: '登录'}},
  {path: '/404', component: _import('public/404'), name: '404', meta: {title: '页面丢失'}},

]
// 主入口路由(需嵌套上左右整体布局)
const mainRoutes = {
  path: '/',
  component: _import('index/index'),
  name: 'index',
  meta: {title: '主入口整体布局'},
  redirect: {name: 'home'},
  children: [
    {path: '/home', component: _import('article/home'), name: 'home', meta: {title: '首页'}},
    {path: '/article-common', component: _import('article/article-common'), name: 'article-common', meta: {title: '普通文本'}}
  ]
}

//获取原型对象上的push函数
const originalPush = Router.prototype.push
//修改原型对象中的push方法
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const router = new Router({
  mode: 'hash',
  scrollBehavior: () => ({y: 0}),
  isAddDynamicMenuRoutes: false, // 是否已经添加动态(菜单)路由
  routes: globalRoutes.concat(mainRoutes)
})
router.beforeEach((to, from, next) => {
    if (to.matched.length === 0) {
      from.name ? next({
        name: from.name
      }) : next('/404')
    }
    else {
      next()
    }

})
export default router
